/*
 * To change this template+ " "+ choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.BranchDAO;
import com.cashmate.hibernate.dao.CashcustodianreportDAO;
import com.cashmate.hibernate.dao.CheckDAO;
import com.cashmate.hibernate.dao.CollectionsDAO;
import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.pojo.Additionalfund;
import com.cashmate.hibernate.pojo.Bankcreditmemo;
import com.cashmate.hibernate.pojo.Bankdebitmemo;
import com.cashmate.hibernate.pojo.Cashcustodianreport;
import com.cashmate.hibernate.pojo.Cashdisbursement;
import com.cashmate.hibernate.pojo.Checks;
import com.cashmate.hibernate.pojo.Collections;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.validator.Valid;

/**
 *
 * @author Sarah
 */
public class PostCCR extends ActionSupport {

    @Valid
    private CashcustodianreportDAO ccrdao = (CashcustodianreportDAO) ServletActionContext.getServletContext().getAttribute("cashcustodianreportDAO");
    private EmployeeDAO empdao = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private CollectionsDAO colldao = (CollectionsDAO) ServletActionContext.getServletContext().getAttribute("collectionsDAO");
    private LoansDAO loandao = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private BranchDAO branchdao = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");
    private CheckDAO checkdao = (CheckDAO) ServletActionContext.getServletContext().getAttribute("checksDAO");
    private String loc;
    private String acctnum, branch, empcode, empname, bankcode, date;
    private int branchid;
    private Integer ccrid;
    private double beginbalcib, depintransitcib, collectioncib, disbursementcib, addtlfundcib, bankdebitmemocib, bankcreditmemocib, endbalcib;
    private double beginbalcoh, depintransitcoh, collectioncoh, disbursementcoh, addtlfundcoh, bankdebitmemocoh, bankcreditmemocoh, endbalcoh;
    private double beginbaltotal, depintransittotal, collectiontotal, disbursementtotal, addtlfundtotal, bankdebitmemototal, bankcreditmemototal, endbaltotal;
    //Deposit In transit
    private int depositslipnum;
    private String datedeposit;
    private double depositamount;
    //Check
//    private String[] checknumber, checkdate;
//    private Integer[] checkid;
//    private Integer[] checkborrower;
//    private int[] checkamount;
    //Cash
    private String[] cashpayeefname, cashpayeemname, cashpayeelname, cashparticular;
    private Integer[] cashid;
    private int[] cashdocnum;
    private double[] cashamount;
    //Additional Funds
    private String[] payeefundfname, payeefundmname, payeefundlname, particularfund;
    private Integer[] fundid;
    private int[] docnumfund;
    private double[] amountfund;
    //Bank Debit Memo
    private Integer[] bankdebitmemoid;
    private String[] particulardebit;
    private double[] amountdebit;
    //Bank Credit Memo
    private Integer[] bankcreditmemoid;
    private String[] particularcredit;
    private double[] amountcredit;
    //Collection
    private Integer[] collectionid;
    //Deposit in Transit
    private Integer[] depositid;
    private String stat;
    //Check
    private Integer[] checkid;

    public String getStat() {
        return stat;
    }

    public void setStat(String stat) {
        this.stat = stat;
    }

    public void setCcrid(int ccrid) {
        this.ccrid = ccrid;
    }

    public void setCollectionid(Integer[] collectionid) {
        this.collectionid = collectionid;
    }

    public void setDepositid(Integer[] depositid) {
        this.depositid = depositid;
    }

    public void setBranchid(int branchid) {
        this.branchid = branchid;
    }

    public void setAcctnum(String acctnum) {
        this.acctnum = acctnum;
    }

    public void setAddtlfundcib(double addtlfundcib) {
        this.addtlfundcib = addtlfundcib;
    }

    public void setAddtlfundcoh(double addtlfundcoh) {
        this.addtlfundcoh = addtlfundcoh;
    }

    public void setAddtlfundtotal(double addtlfundtotal) {
        this.addtlfundtotal = addtlfundtotal;
    }

    public void setAmountdebit(double[] amountdebit) {
        this.amountdebit = amountdebit;
    }

    public void setAmountfund(double[] amountfund) {
        this.amountfund = amountfund;
    }

    public void setBankcode(String bankcode) {
        this.bankcode = bankcode;
    }

    public void setBankdebitmemocib(double bankdebitmemocib) {
        this.bankdebitmemocib = bankdebitmemocib;
    }

    public void setBankdebitmemocoh(double bankdebitmemocoh) {
        this.bankdebitmemocoh = bankdebitmemocoh;
    }

    public void setBankdebitmemototal(double bankdebitmemototal) {
        this.bankdebitmemototal = bankdebitmemototal;
    }

    public void setBeginbalcib(double beginbalcib) {
        this.beginbalcib = beginbalcib;
    }

    public void setBeginbalcoh(double beginbalcoh) {
        this.beginbalcoh = beginbalcoh;
    }

    public void setBeginbaltotal(double beginbaltotal) {
        this.beginbaltotal = beginbaltotal;
    }

    public void setBranch(String branch) {
        this.branch = branch;
    }

    public void setCashamount(double[] cashamount) {
        this.cashamount = cashamount;
    }

    public void setCashdocnum(int[] cashdocnum) {
        this.cashdocnum = cashdocnum;
    }

    public void setCashparticular(String[] cashparticular) {
        this.cashparticular = cashparticular;
    }

    public void setCashpayeefname(String[] cashpayeefname) {
        this.cashpayeefname = cashpayeefname;
    }

    public void setCashpayeelname(String[] cashpayeelname) {
        this.cashpayeelname = cashpayeelname;
    }

    public void setCashpayeemname(String[] cashpayeemname) {
        this.cashpayeemname = cashpayeemname;
    }

    public void setPayeefundfname(String[] payeefundfname) {
        this.payeefundfname = payeefundfname;
    }

    public void setPayeefundlname(String[] payeefundlname) {
        this.payeefundlname = payeefundlname;
    }

    public void setPayeefundmname(String[] payeefundmname) {
        this.payeefundmname = payeefundmname;
    }

    public void setCollectioncib(double collectioncib) {
        this.collectioncib = collectioncib;
    }

    public void setCollectioncoh(double collectioncoh) {
        this.collectioncoh = collectioncoh;
    }

    public void setCollectiontotal(double collectiontotal) {
        this.collectiontotal = collectiontotal;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setDatedeposit(String datedeposit) {
        this.datedeposit = datedeposit;
    }

    public void setDepintransitcib(double depintransitcib) {
        this.depintransitcib = depintransitcib;
    }

    public void setDepintransitcoh(double depintransitcoh) {
        this.depintransitcoh = depintransitcoh;
    }

    public void setDepintransittotal(double depintransittotal) {
        this.depintransittotal = depintransittotal;
    }

    public void setDepositamount(double depositamount) {
        this.depositamount = depositamount;
    }

    public void setDepositslipnum(int depositslipnum) {
        this.depositslipnum = depositslipnum;
    }

    public void setDisbursementcib(double disbursementcib) {
        this.disbursementcib = disbursementcib;
    }

    public void setDisbursementcoh(double disbursementcoh) {
        this.disbursementcoh = disbursementcoh;
    }

    public void setDisbursementtotal(double disbursementtotal) {
        this.disbursementtotal = disbursementtotal;
    }

    public void setDocnumfund(int[] docnumfund) {
        this.docnumfund = docnumfund;
    }

    public void setEmpcode(String empcode) {
        this.empcode = empcode;
    }

    public void setEmpname(String empname) {
        this.empname = empname;
    }

    public void setEndbalcib(double endbalcib) {
        this.endbalcib = endbalcib;
    }

    public void setEndbalcoh(double endbalcoh) {
        this.endbalcoh = endbalcoh;
    }

    public void setEndbaltotal(double endbaltotal) {
        this.endbaltotal = endbaltotal;
    }

    public void setParticulardebit(String[] particulardebit) {
        this.particulardebit = particulardebit;
    }

    public void setParticularfund(String[] particularfund) {
        this.particularfund = particularfund;
    }

    public void setAmountcredit(double[] amountcredit) {
        this.amountcredit = amountcredit;
    }

    public void setBankcreditmemocib(double bankcreditmemocib) {
        this.bankcreditmemocib = bankcreditmemocib;
    }

    public void setBankcreditmemocoh(double bankcreditmemocoh) {
        this.bankcreditmemocoh = bankcreditmemocoh;
    }

    public void setBankcreditmemototal(double bankcreditmemototal) {
        this.bankcreditmemototal = bankcreditmemototal;
    }

    public void setParticularcredit(String[] particularcredit) {
        this.particularcredit = particularcredit;
    }

    public String getLoc() {
        return loc;
    }

    public void setBankcreditmemoid(Integer[] bankcreditmemoid) {
        this.bankcreditmemoid = bankcreditmemoid;
    }

    public void setBankdebitmemoid(Integer[] bankdebitmemoid) {
        this.bankdebitmemoid = bankdebitmemoid;
    }

    public void setCashid(Integer[] cashid) {
        this.cashid = cashid;
    }

    public void setCheckid(Integer[] checkid) {
        this.checkid = checkid;
    }

    public void setFundid(Integer[] fundid) {
        this.fundid = fundid;
    }

    @Action(value = "/post-ccr", results = {
        @Result(name = "input", location = "${loc}"),
        @Result(name = "success", location = "${loc}")
    })
    @Override
    public String execute() {
        try {
            System.out.println("ccrid" + ccrid);
            loc = "../../ccr.jsp";
            System.out.println("SaveCCR:");

            Cashcustodianreport cashcustodian = new Cashcustodianreport();
            cashcustodian.setCcrid(ccrid);
            cashcustodian.setAddtfundtotalcash(addtlfundtotal);
            cashcustodian.setAddtlfundcib(addtlfundcib);
            cashcustodian.setAddtlfundcoh(addtlfundcoh);
            cashcustodian.setBankaccountnum(acctnum);
            cashcustodian.setBankcode(bankcode);
            cashcustodian.setBankdebitcib(bankdebitmemocib);
            cashcustodian.setBankdebitcoh(bankdebitmemocoh);
            cashcustodian.setBankdebittotalcash(bankdebitmemototal);
            cashcustodian.setBeginbalcib(beginbalcib);
            cashcustodian.setBeginbalcoh(beginbalcoh);
            cashcustodian.setBeginbaltotalcash(beginbaltotal);
            cashcustodian.setBranches(branchdao.get(branchid));
            cashcustodian.setCcrdate(date);
            cashcustodian.setCollcib(collectioncib);
            cashcustodian.setCollcoh(collectioncoh);
            cashcustodian.setColltotalcash(collectiontotal);
            cashcustodian.setDepositamount(depositamount);
            cashcustodian.setDepositcib(depintransitcib);
            cashcustodian.setDepositcoh(depintransitcoh);
            cashcustodian.setDepositdate(datedeposit);
            cashcustodian.setDepositno(depositslipnum);
            cashcustodian.setDeposittotalcash(depintransittotal);
            cashcustodian.setDisbursementcib(disbursementcib);
            cashcustodian.setDisbursementcoh(disbursementcoh);
            cashcustodian.setDisbursementtotalcash(disbursementtotal);
            cashcustodian.setEmployees(empdao.getByIdNumber(empcode));
            cashcustodian.setEndbalcib(endbalcib);
            cashcustodian.setEndbalcoh(endbalcoh);
            cashcustodian.setEndbaltotalcash(endbaltotal);
            cashcustodian.setBankcreditcib(bankcreditmemocib);
            cashcustodian.setBankcreditcoh(bankcreditmemocoh);
            cashcustodian.setBankcredittotalcash(bankcreditmemototal);
            cashcustodian.setStat("posted");

//            List<Checks> checklist = new ArrayList<Checks>();
//            Checks check = null;
//            if (checkid.length > checkborrower.length) {    //delete
//                for (int j = 0; j < checkid.length; j++) {
//                    check = new Checks();
//                    check.setCheckid(checkid[j]);
//                    if (j < checkborrower.length) {
//                        check.setBankaccountno(acctnum);
//                        check.setCheckamount(checkamount[j]);
//                        check.setCheckdate(checkdate[j]);
//                        check.setCheckno(checknumber[j]);
//                        check.setLoans(loandao.getLoansByBorrowerUnique(checkborrower[j]));
//                    } else {
//                        check.setBankaccountno(0 + "");
//                        check.setCheckamount(0);
//                        check.setCheckdate("");
//                        check.setCheckno(0 + "");
//                        check.setLoans(null);
//                    }
//                    checklist.add(check);
//                }
//            } else {        //add or no change
//                for (int j = 0; j < checkborrower.length; j++) {
//                    check = new Checks();
//                    if (j < checkid.length) {
//                        check.setCheckid(checkid[j]);
//                    }
//                    check.setBankaccountno(acctnum);
//                    check.setCheckamount(checkamount[j]);
//                    check.setCheckdate(checkdate[j]);
//                    check.setCheckno(checknumber[j]);
//                    check.setLoans(loandao.getLoansByBorrowerUnique(checkborrower[j]));
//                    checklist.add(check);
//                }
//            }

            List<Cashdisbursement> cashlist = new ArrayList<Cashdisbursement>();
            Cashdisbursement cash = null;
            if (cashid.length > cashpayeelname.length) {//delete
                for (int j = 0; j < cashid.length; j++) {
                    cash = new Cashdisbursement();
                    cash.setCashid(cashid[j]);

                    if (j < cashpayeelname.length) {
                        cash.setAmount(cashamount[j]);
                        cash.setDocnum(cashdocnum[j]);
                        cash.setParticulars(cashparticular[j]);
                        cash.setPayeeFirstname(cashpayeefname[j]);
                        cash.setPayeeLastname(cashpayeelname[j]);
                        cash.setPayeeMiddlename(cashpayeemname[j]);
                        cash.setCashcustodianreport(cashcustodian);
                    } else {
                        cash.setAmount(0);
                        cash.setDocnum(0);
                        cash.setParticulars("");
                        cash.setPayeeFirstname("");
                        cash.setPayeeLastname("");
                        cash.setPayeeMiddlename("");
                        cash.setCashcustodianreport(null);
                    }
                    cashlist.add(cash);
                }
            } else {    //add or no change
                for (int j = 0; j < cashpayeelname.length; j++) {
                    cash = new Cashdisbursement();
                    if (j < cashid.length) {
                        cash.setCashid(cashid[j]);
                    }
                    cash.setAmount(cashamount[j]);
                    cash.setDocnum(cashdocnum[j]);
                    cash.setParticulars(cashparticular[j]);
                    cash.setPayeeFirstname(cashpayeefname[j]);
                    cash.setPayeeLastname(cashpayeelname[j]);
                    cash.setPayeeMiddlename(cashpayeemname[j]);
                    cash.setCashcustodianreport(cashcustodian);
                    cashlist.add(cash);
                }
            }


            List<Additionalfund> addtlfundlist = new ArrayList<Additionalfund>();
            Additionalfund addtlfund = null;
            if (fundid.length > payeefundlname.length) {//delete

                for (int j = 0; j < fundid.length; j++) {
                    addtlfund = new Additionalfund();
                    addtlfund.setFundid(fundid[j]);

                    if (j < payeefundlname.length) {
                        addtlfund.setAmount(amountfund[j]);
                        addtlfund.setDocnum(docnumfund[j]);
                        addtlfund.setParticulars(particularfund[j]);
                        addtlfund.setPayeeFirstname(payeefundfname[j]);
                        addtlfund.setPayeeLastname(payeefundlname[j]);
                        addtlfund.setPayeeMiddlename(payeefundmname[j]);
                        addtlfund.setCashcustodianreport(cashcustodian);
                    } else {
                        addtlfund.setAmount(0);
                        addtlfund.setDocnum(0);
                        addtlfund.setParticulars("");
                        addtlfund.setPayeeFirstname("");
                        addtlfund.setPayeeLastname("");
                        addtlfund.setPayeeMiddlename("");
                        addtlfund.setCashcustodianreport(null);
                    }
                    addtlfundlist.add(addtlfund);
                }
            } else {//add or no change

                for (int j = 0; j < payeefundlname.length; j++) {
                    addtlfund = new Additionalfund();
                    if (j < fundid.length) {
                        addtlfund.setFundid(fundid[j]);
                    }
                    addtlfund.setAmount(amountfund[j]);
                    addtlfund.setDocnum(docnumfund[j]);
                    addtlfund.setParticulars(particularfund[j]);
                    addtlfund.setPayeeFirstname(payeefundfname[j]);
                    addtlfund.setPayeeLastname(payeefundlname[j]);
                    addtlfund.setPayeeMiddlename(payeefundmname[j]);
                    addtlfund.setCashcustodianreport(cashcustodian);
                    addtlfundlist.add(addtlfund);
                }
            }


            List<Bankdebitmemo> bankdebitlist = new ArrayList<Bankdebitmemo>();
            Bankdebitmemo bankdebit = null;
            if (bankdebitmemoid.length > particulardebit.length) {      //delete
                for (int j = 0; j < bankdebitmemoid.length; j++) {
                    bankdebit = new Bankdebitmemo();
                    bankdebit.setBankdebitmemoid(bankdebitmemoid[j]);

                    if (j < particulardebit.length) {
                        bankdebit.setParticulars(particulardebit[j]);
                        bankdebit.setAmount(amountdebit[j]);
                        bankdebit.setCashcustodianreport(cashcustodian);
                    } else {
                        bankdebit.setParticulars("");
                        bankdebit.setAmount(0);
                        bankdebit.setCashcustodianreport(null);
                    }
                    bankdebitlist.add(bankdebit);
                }
            } else {//add or no change
                for (int j = 0; j < particulardebit.length; j++) {
                    bankdebit = new Bankdebitmemo();
                    if (j < bankdebitmemoid.length) {
                        bankdebit.setBankdebitmemoid(bankdebitmemoid[j]);
                    }
                    bankdebit.setParticulars(particulardebit[j]);
                    bankdebit.setAmount(amountdebit[j]);
                    bankdebit.setCashcustodianreport(cashcustodian);
                    bankdebitlist.add(bankdebit);
                }
            }



            List<Bankcreditmemo> bankcreditlist = new ArrayList<Bankcreditmemo>();
            Bankcreditmemo bankcredit = null;
            if (bankcreditmemoid.length > particularcredit.length) {    //delete
                for (int j = 0; j < bankcreditmemoid.length; j++) {
                    bankcredit = new Bankcreditmemo();
                    bankcredit.setBankcreditmemoid(bankcreditmemoid[j]);

                    if (j < particularcredit.length) {
                        bankcredit.setParticulars(particularcredit[j]);
                        bankcredit.setAmount(amountcredit[j]);
                        bankcredit.setCashcustodianreport(cashcustodian);
                    } else {
                        bankcredit.setParticulars("");
                        bankcredit.setAmount(0);
                        bankcredit.setCashcustodianreport(null);
                    }
                    bankcreditlist.add(bankcredit);
                }
            } else {//add or no change
                for (int j = 0; j < particularcredit.length; j++) {
                    bankcredit = new Bankcreditmemo();
                    if (j < bankcreditmemoid.length) {
                        bankcredit.setBankcreditmemoid(bankcreditmemoid[j]);
                    }
                    bankcredit.setParticulars(particularcredit[j]);
                    bankcredit.setAmount(amountcredit[j]);
                    bankcredit.setCashcustodianreport(cashcustodian);
                    bankcreditlist.add(bankcredit);
                }
            }


            List<Collections> collectionlist = new ArrayList<Collections>();

            if (collectionid != null) {
                for (int j = 0; j < collectionid.length; j++) {
                    collectionlist.add(colldao.get(collectionid[j]));
                }
            }

            List<Collections> depositlist = new ArrayList<Collections>();
            if (depositid != null) {
                for (int j = 0; j < depositid.length; j++) {
                    depositlist.add(colldao.get(depositid[j]));
                }
            }


            List<Checks> checklist = new ArrayList<Checks>();

            if (checkid != null) {
                for (int j = 0; j < checkid.length; j++) {
                    checklist.add(checkdao.get(checkid[j]));
                }
            }
            
            stat = ccrdao.postCCR(cashcustodian, checklist, cashlist, addtlfundlist, bankdebitlist, bankcreditlist, collectionlist, depositlist);
            ServletActionContext.getRequest().setAttribute("stat", stat);

            System.out.println("fdskjh");
            return SUCCESS;
        } catch (Exception e) {
            stat = e.toString();
            System.out.println("a:" + e.toString());
            e.printStackTrace();
        }
        return SUCCESS;
    }
}
